package com.example.demo.easyexcel;

import com.alibaba.excel.util.CollectionUtils;
import com.example.demo.easyexcel.common.UtilEasypoi;
import com.example.demo.easyexcel.entity.User;
import com.example.demo.easyexcel.service.ImportService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author dbin
 * @Date 2019/12/27
 **/

@RequestMapping(WEB1.BASE_PATH)
@RestController
public class WEB1 {

    public static final String BASE_PATH = "/api/excel";

    @Resource
    private ImportService importService;


    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("index");
    }

    @PostMapping("batch-upload")
    public String batchImport(MultipartFile file) {
        //1、第一步获取到表格中的数据
        List<User> imports = UtilEasypoi.importExcel(file, 0, 1, User.class);
        if (CollectionUtils.isEmpty(imports)) {
            return "获取不到数据，请检查上传文件！";
        }

        int notSSONumber = 0;
        int existAdminNumber = 0;

        List<User> users = new ArrayList<>();
        StringBuilder result=new StringBuilder();
        for (User user : imports) {

            result.append(user.toString());
            if(2>1){
                users.add(user);
            }

        }



        //6、组织返回信息
         String message = "--------------------------导入成功，excel数据总共" + imports.size() + "条，导入成功：" + users.size() + "条";
        if (notSSONumber != 0) {
            message += "，用户信息不存在认证库：" + notSSONumber + "条";
        }
        if (existAdminNumber != 0) {
            message += "，用户信息已存在用户信息库：" + existAdminNumber + "条";
        }
        message += "。";

        return result + message ;
    }

/*    @PostMapping(value = "upload")
    public String uploadExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("file");
        if (file.isEmpty()) {
            return "文件不能为空";
        }
        InputStream inputStream = file.getInputStream();
        List<List<Object>> list = importService.getBankListByExcel(inputStream, file.getOriginalFilename());
        inputStream.close();

        for (int i = 0; i < list.size(); i++) {
            List<Object> lo = list.get(i);
            //TODO 随意发挥
            System.out.println(lo);

        }
        return "上传成功";
    }*/
/*

    @RequestMapping("build")
    public void contextLoads() {
        // 文件输出位置
        OutputStream out = null;
        try {
            out = new FileOutputStream("D:\\Temp\\test.xlsx");
            ExcelWriter writer = EasyExcelFactory.getWriter(out);

            // 写仅有一个 Sheet 的 Excel 文件, 此场景较为通用
            Sheet sheet1 = new Sheet(1, 0, User.class);

            // 第一个 sheet 名称
            sheet1.setSheetName("第一个sheet");

            // 写数据到 Writer 上下文中
            // 入参1: 数据库查询的数据list集合
            // 入参2: 要写入的目标 sheet
            writer.write(getAllUser(), sheet1);

            // 将上下文中的最终 outputStream 写入到指定文件中
            writer.finish();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭流
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    public List<User> getAllUser(){
        List<User> userList = new ArrayList<>();
        for (int i=0;i<10;i++){
//            User user = User.builder().name("张三"+ i).password("1234").age(i).build();
            User user =new User();
            user.setName("张三"+i);
            user.setPassword("123");
            user.setAge(i);
            userList.add(user);
        }
        return userList;
    }
*/

}
